import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

//引入公共样式
import "./assets/css/common.css"
//引入图标
import "./assets/iconfont/阿里云图标/阿里云图标_/iconfont.css"

import "@/assets/css/el-reset.css"

//进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'


import qfSubMenu from "qf-sub-menu" //引入我们的组件
Vue.use(qfSubMenu)

//路由守卫
router.beforeEach((to,from,next) =>{
  //进度条打开
  NProgress.start(); //进度条出现

   //console.log(to);
  // console.log(from);
  // console.log(next);
  //判断存储中是否有token，有的话就放行，没有就重新跳转到登入页
  let token = localStorage.getItem("bb-token")
  if(token){
    if(store.state.userMenu.length === 0){
      //用户的路由信息（菜单信息）没有了，需要重新发起请求
      store.dispatch('FETCH_MENULIST')
      .then(() => {
        // 重新激活动态添加的路由，replace: true 不产生历史记录
        next({path:to.path,replace: true})
      })
    }else{
      next()
    }
  }else{
    //用户没有登入不能访问登入页面以外的页面‘
    if(to.path ==="/login"){
      next()
    }else{
      next({path:"/login"})
    }
  }
})


router.afterEach((to,next)=>{
  NProgress.done();  //进度条消失
})


Vue.config.productionTip = false;
Vue.use(ElementUI);


import "./assets/css/common.css"
new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
